<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="searchForm">
				<el-form-item>
					<el-input v-model="searchForm.username" placeholder="用户账号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" :stripe="true">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index">
			</el-table-column>
			<el-table-column prop="username" label="用户账号" align="center">
			</el-table-column>
			<el-table-column prop="nickname" label="用户昵称"  align="center">
			</el-table-column>
			<el-table-column prop="state" label="用户状态" align="center">
				<template scope="scope">
					<span v-if="scope.row.state == 0" style="color: blue">待激活</span>
					<span v-if="scope.row.state == -1" style="color: red">禁用</span>
					<span v-if="scope.row.state == 1" style="color: green">激活</span>
				</template>
			</el-table-column>
			<el-table-column prop="createtime" label="注册时间"  align="center">
				<template slot-scope="scope">
					<span>
						{{operatingTime(scope.row.createtime)}}
					</span>
				</template>
			</el-table-column>
			<el-table-column prop="email" label="用户邮箱"  align="center">
			</el-table-column>
			<el-table-column prop="phone" label="用户电话"  align="center">
			</el-table-column>
			<el-table-column prop="age" label="用户年龄"  align="center">
			</el-table-column>
			<el-table-column label="操作" align="center">
				<template scope="scope">
					<el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增编辑公共界面-->
		<el-dialog :title="title" v-model="UserFormVisible" :close-on-click-modal="false" v-loading="UserLoading" >
			<el-form :model="UserForm" label-width="80px" :rules="UserFormRules" ref="UserForm">
				<el-form-item label="用户账号" prop="username">
					<el-input v-model="UserForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="用户邮箱" prop="email">
					<el-input v-model="UserForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="用户电话" prop="phone">
					<el-input v-model="UserForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="用户年龄" prop="age">
					<el-input v-model.number="UserForm.age" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="用户状态">
					<el-radio-group v-model="UserForm.state">
						<el-radio class="radio" :label = -1 >禁用</el-radio>
						<el-radio class="radio" :label = 1 >激活</el-radio>
						<el-radio class="radio" :label = 0 >待激活</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" align="center">
				<el-button type="danger" @click.native="UserFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="UserSubmit" :loading="UserLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';
	export default {
		data() {
			return {
                searchForm: {
					username: ''
				},
				employees: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				UserFormVisible: false,//编辑界面是否显示
				UserLoading: false,
				UserFormRules: {
				},
				//新增编辑界面数据
				UserForm: {
					username: '',
					nickname:'',
					state:'',
					age: '',
					phone: '',
					email:'',
				},
				title:'',
			}
		},
		methods: {
			operatingTime(timeDate){
				return this.$moment(timeDate).format('YYYY-MM-DD')
			},
			handleCurrentChange(val) {//获取所在页码
				this.page = val;
				this.getUsers();//点击页码时调用钩子方法
			},
			//获取用户列表
			getUsers() {
				let para = {
					currentPage: this.page,
					keywords:this.searchForm.username
				};
				//打开加载转圈圈的效果
				this.listLoading = true;
				//查询数据
				this.$http.post("/user",para).then(res=>{
				    this.employees = res.data.data;
				    this.total=res.data.total;
                    //关闭加载的效果
                    this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					this.$http.delete("/user/"+para.id).then((res) => {
						this.listLoading = false;
						if (res.data.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						}else {
							this.$message({
								message: "删除失败"+res.data.message,
								type: 'warning'
							});
						}
						this.getUsers();
					});
				}).catch(() => {
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.UserFormVisible = true;
				this.title='编辑';
				this.UserForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.UserFormVisible = true;
				this.title='新增';
				this.UserForm = {
				};
			},
			//编辑和新增提交
			UserSubmit: function () {
				//验证当前的表单是否符合验证规则
				this.$refs.UserForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.UserLoading = true;
							//把表单的数据组装为json
							let para = Object.assign({}, this.UserForm);
							//console.debug(para)
							this.$http.put('/user',para).then((res) => {
								this.UserLoading = false;
								if (res.data.success){
									this.$message({
										message: '提交成功',
										type: 'success'
									});
								}else {
									this.$message({
										message: "更新失败"+res.data.message,
										type: 'warning'
									});
								}
								//重置表单-调用现成的方法 清空数据
								this.$refs['UserForm'].resetFields();
								//关闭 更新界面
								this.UserFormVisible = false;
								//重新刷新页面数据 钩子方法
								this.getUsers();
							});
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				var names = this.sels.map(item => item.name).toString();
				this.$confirm('确认删除选中记录 '+ names +' 吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					console.debug(ids);
					let para = { ids: ids };
					this.$http.post('/user/dels',ids).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						if (res.data.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						}else {
							this.$message({
								message: "删除失败"+res.data.message,
								type: 'warning'
							});
						}
						this.getUsers();
					});
				}).catch(() => {
				});
			}
		},
		mounted() {
		    //当页面加载完以后。执行的方法
			this.getUsers();
		}
	}
</script>
<style scoped>
</style>